/*************************************************************************
 *                                                                       *
 * This file is part of the FreeMAPs distribution.  For full copyright   *
 * information, see COPYRIGHT and COPYING.LESSER.                        *
 *                                                                       *
 * Author:           sqbang                                              *
 * Email:            sqbang@ustc.edu                                     *
 * Date:             2013.10.01                                          *
 *                                                                       *
 *************************************************************************/

//declaration header
#include "COMMON/enum/Axis.h"

//COMMON headers
#include "COMMON/info/InfoManager.h"
#include "COMMON/info/CommonInfo.h"
#include "COMMON/tbox/StringToEnum.h"
#include "COMMON/tbox/Bimap.h"
#include "COMMON/tbox/Utilities.h"
#include "COMMON/tbox/LogMessage.h"

namespace FreeMAPs
{

namespace Utilities
{

template<>
void
init_string_to_enum(
    Bimap<std::string, Axis>& se)
{
    se.clear();

    se.insert ("X", Axis::X);
    se.insert ("Y", Axis::Y);
    se.insert ("Z", Axis::Z);

    se.insert ("INVALID_VALUE", Axis::INVALID_VALUE);
}



std::string
int_to_axis (
    const int num,
    const bool use_capital)
{
    switch (num)
    {
    case 0:
        {
            return (use_capital ? "X" : "x");
        }
    case 1:
        {
            return (use_capital ? "Y" : "y");
        }
    case 2:
        {
            return (use_capital ? "Z" : "z");
        }
    default:
        {
            FREEMAPS_FIND_BUG();
            return FREEMAPS_INVALID_STRING_VALUE;
        }
    }
}



int
axis_to_int (
    const std::string& axis)
{
    if (axis == "x" || axis == "X")
    {
        return 0;
    }
    else if (axis == "y" || axis == "Y")
    {
        return 1;
    }
    else if (axis == "z" || axis == "Z")
    {
        return 2;
    }
    else
    {
        return FREEMAPS_INVALID_VALUE;
    }
}



void
check_axis (
    const InputDatabase* db,
    const std::string& key,
    const Axis axis)
{
    const int dim = InfoManager::get_instance()->get_common_info()->get_dim();
    if (Utilities::is_invalid_value (axis))
    {
        LogMessage::unknown_error (
            db,
            key);
    }

    if (static_cast<int>(axis) >= dim)
    {
        LogMessage::use_improper_string_value (
            db,
            key,
            enum_to_string (axis),
            "it exceed the dim");
    }
}

} //namespace Utilities

} //namespace FreeMAPs
